#include "keythread.h"

#define TIME_KEY 10

KeyThread::KeyThread()
{

}

void KeyThread::run()
{
    int max_fd = -1;
#if 1
    system("echo 192 > /sys/class/gpio/export");
    system("echo 194 > /sys/class/gpio/export");
    system("echo 195 > /sys/class/gpio/export");

    system("echo in > /sys/class/gpio/gpio192/direction");
    system("echo in > /sys/class/gpio/gpio194/direction");
    system("echo in > /sys/class/gpio/gpio195/direction");
#endif

    int gpio192 = open("/sys/class/gpio/gpio192/value",O_RDWR);
    if(gpio192 == -1)
    {
        qDebug("open gpio192 error\n");
        return ;
    }
    max_fd = gpio192;

    int gpio194 = open("/sys/class/gpio/gpio194/value",O_RDWR);
    if(gpio194 == -1)
    {
        qDebug("open error\n");
        return ;
    }
    if(gpio194 > max_fd)
        max_fd = gpio194;

    int gpio195 = open("/sys/class/gpio/gpio195/value",O_RDWR);
    if(gpio195 == -1)
    {
        qDebug("open error\n");
        return ;
    }
    if(gpio195 > max_fd)
        max_fd = gpio194;

    fd_set fds;

    struct timeval time;
    time.tv_sec = 2;
    time.tv_usec = 0;
    int num = 0;

    char buf[10] = {0};

    bool pressL = false;
    bool pressL_long = false;
    int countL = 0;

    bool pressC = false;
    bool pressC_long = false;
    int countC = 0;

    bool pressR = false;
    bool pressR_long = false;
    int countR = 0;
    while (1)
    {
        readKey_left(gpio192, pressL, pressL_long, countL, 0);
        readKey_left(gpio194, pressC, pressC_long, countC, 1);
        readKey_left(gpio195, pressR, pressR_long, countR, 2);
#if 0
        memset(buf, 0, sizeof(buf));
        lseek(gpio192, 0, SEEK_SET);
        int ret = read(gpio192, buf,sizeof(buf));
        if(strstr(buf, "0") != NULL)
        {
            emit key_press(0);
        }
        else if(ret<0)
        {
            perror("read error\n");
        }
        else
        {
            qDebug() << "192 : " << buf;
            memset(buf, 0, sizeof(buf));

            lseek(gpio194, 0, SEEK_SET);
            read(gpio194,buf,sizeof(buf));
            if(strstr(buf, "0") != NULL)
            {
                emit key_press(1);
            }
            else
            {
                lseek(gpio195, 0, SEEK_SET);
                read(gpio195,buf,sizeof(buf));
                if(strstr(buf, "0") != NULL)
                {
                    emit key_press(2);
                }
            }
        }
#endif
        msleep(TIME_KEY);
    }
}

void KeyThread::readKey_left(int &fd, bool &press, bool &pressLong, int &count, int key)
{
    char buf[10] = {0};
    memset(buf, 0, sizeof(buf));
    lseek(fd, 0, SEEK_SET);
    int ret = read(fd, buf,sizeof(buf));
    if(strstr(buf, "0") != NULL)
    {
        //emit key_press(0);
        if(!press)
        {
            emit keyEvent(KEY_PRESS, key);
            press = true;
        }
        else
        {
            count++;

            if(count >= (1000 / TIME_KEY))
            {
                if(!pressLong)
                {
                    emit keyEvent(KEY_LONG_PRESS, key);
                }

                pressLong = true;
            }
        }
    }
    else if(ret<0)
    {
        perror("read error\n");
        if(press)
        {
            emit keyEvent(KEY_RELEASE, key);
            if(!pressLong)
            {
                emit keyEvent(KEY_CLICK, key);
            }
        }

        press = false;
        pressLong = false;
    }
    else
    {
        if(press)
        {
            emit keyEvent(KEY_RELEASE, key);
            if(!pressLong)
            {
                emit keyEvent(KEY_CLICK, key);
            }
        }

        press = false;
        pressLong = false;
    }
}
